import 'package:get/get.dart';
import '../../../utils/storage/userService.dart';
import '../../../models/user_model.dart';

class UserController extends GetxController {
  //TODO: Implement UserController
  RxBool isLogin = false.obs;
  Rx<UserModel> userInfo = UserModel().obs;
  @override
  void onInit() {
    super.onInit();
    getUserinfo();
  }

  // 获取用户信息以及登录状态
  getUserinfo() async {
    isLogin.value = await UserService.getUserLoginState();
    List userInfoList = await UserService.getUserInfo();
    if (userInfoList.isNotEmpty) {
      userInfo.value = UserModel.fromJson(userInfoList[0]);
      update();
    }
  }
  // 登录

  // 退出登录
  void logout() async {
    await UserService.removeUserInfo("userinfo");
    userInfo.value = UserModel(); // 清空用户信息
    isLogin.value = false;
    update();
  }

  @override
  void onClose() {
    super.onClose();
  }
}
